app.controller("z-codeImport-c", [ '$scope','$rootScope','$z-http','$sce','z-codeImport-s','z-edit-menu-s','z-systemConfigure-s','$filter','$location','downloadUrl',
		function($scope,$rootScope,$zhttp,$sce,Service,MenuService,SystemConfigureService,$filter,$location,$downloadUrl) {
		$scope.baseUrl = $location.protocol()+"://"+$location.host()+":"+$location.port()+$downloadUrl;
		$scope.importModel = {};
        $scope.viewModel   = {};

        $scope.importModel.attachmentId = null;
        $scope.importModel.uploadStream = {};
        $scope.importModel.messageHideType=false;
        $scope.importModel.successHideType=false;
        $scope.importModel.message=null;
        $scope.importModel.isArray=false;
        $scope.importModel.radioValue="2";
        $scope.viewModel.effectiveDate=new Date();
		//查询页面名称
		MenuService.getMenuByUrl({url:'codeImport'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});

		
        $scope.importModel.getTemplate = function () {
            $rootScope.isLoading = true;
            Service.getTemplateUrl(function (data) {
                $rootScope.isLoading = false;
                window.location.href       = data;
            }, function () {
                $rootScope.isLoading = false;
                //$rootScope.alertError("系统错误!");
                $scope.importModel.messageHideType=true;
                $scope.importModel.message="系统错误";
                $scope.importModel.isArray=false;
            });
        };
        $scope.importModel.getEffectiveDate = function () {
            SystemConfigureService.getEffectiveDate({},
    			function(data){
	            	var dt=new Date(data);
					$scope.importModel.effectiveDate = dt;
					$scope.viewModel.effectiveDate=dt;
    			},function(error) {
    				$rootScope.alertError(error.fault.message);
    			});
    	};
    	$scope.importModel.getEffectiveDate();
        $scope.importModel.unload = function () {
            $scope.importModel.attachmentId = null;
            $scope.importModel.uploadStream.cancel();
        };

        $scope.importModel.fileUploadComplete    = function (data) {
            $scope.importModel.messageHideType=false;
            $scope.importModel.successHideType=false;
            var dataList=data;
            if (dataList[0]['attachment'] != null) {
                $scope.$apply(function () {
                    $scope.importModel.attachmentId = dataList[0]['attachment']['attachmentId'];
                    $scope.importModel.save();
                });
            }
        };

        $scope.importModel.save = function () {
        	if($scope.importModel.radioValue==3){
        		if($scope.viewModel.effectiveDate>$scope.importModel.effectiveDate){
        			$rootScope.alertWarning("架构变更时间不得小于上次架构变更时间及当天！");  
        			return true;
        		}
        	}
            var str="确定是否增量导入！";
            if($scope.importModel.radioValue=="1"){
                str="覆盖导入会根据编码替换原有信息，确认是否导入！";
            }else if($scope.importModel.radioValue=="3"){
            	str="架构变更，确认是否导入！";
            }
            $rootScope.alertConfirm(str,
                function() {
                    if ($scope.importModel.attachmentId != null) {
                        $rootScope.isLoading = true;
                        Service.importCode({
                            attachmentId: $scope.importModel.attachmentId,
                            type:$scope.importModel.radioValue,
                            fromDate:$scope.importModel.effectiveDate,
                        }, function (data) {
                            $rootScope.isLoading = false;
                            if (data['result'] == 1) {
                                $scope.importModel.successHideType=true;
                                if(data['success'].length<=0){
                                    $scope.importModel.isArray = false;
                                    $scope.importModel.message = "基础信息导入完成。";
                                }else{
                                    $scope.importModel.isArray = true;
                                    $scope.importModel.message = data['success'];
                                }
                                $scope.importModel.unload();
                            } else {
                                $scope.importModel.messageHideType=true;
                                $scope.importModel.message="系统错误";
                                $scope.importModel.isArray=false;
                            }
                        }, function (data) {
                            $rootScope.isLoading      = false;
                            $scope.importModel.attachmentId = null;
                            $scope.importModel.uploadStream.cancel();
                            $scope.importModel.messageHideType=true;
                            if (angular.isArray(data['fault'])) {
                                var faultMessage = [data['fault'][0]['message']].concat(data['fault'][0]['errorData']);
                                $scope.importModel.message=faultMessage;
                                $scope.importModel.isArray=true;
                                //$rootScope.alertError(faultMessage);
                            } else {
                                $scope.importModel.message=data['fault']['message'];
                                $scope.importModel.isArray=false;
                                //$rootScope.alertError(data['fault']['message']);
                            }
                        });
                    } else {
                        $scope.importModel.message="请先上传文件!";
                        $scope.importModel.isArray=false;
                    }
                });
        };
        $scope.importModel.effectiveDateClose=function(){
        	if($scope.importModel.radioValue==3){
        		
        	}else{
        		$scope.importModel.effectiveDate=null;
        	}
        }

		//日期控件
		$scope.importModel.dateOptions = {
				formatYear: 'yy',
			    startingDay: 1,
			    showWeeks:false
		};
		//日期控件
		$scope.importModel.format = "yyyy/MM/dd";
		//日期启动开关
		$scope.importModel.effectiveDateOpen = false;
		$scope.importModel.effectiveDateOpnened = false;
		$scope.importModel.effectiveDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			$scope.importModel.effectiveDateOpnened = !$scope.importModel.effectiveDateOpnened;

			
		};
} ]);